﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace Candy.Model
{
    public class CandyImportService : IImportService
    {
        private string[] _requiredHeader = new string[]
        {
            "familypath",
            "id",
            "name",
            "species",
         //   "database",
            "sequence",
            "description"
        };

        public Task<ImportReadResult> ReadAsync(string importText)
        {
            return Task.Factory.StartNew<ImportReadResult>(() =>
                  {
                      List<ImportItem> ret = new List<ImportItem>();
                      ImportReadResult irr = new ImportReadResult();
                      irr.Items = ret;
                      string[] lines = importText.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                      var headerBuilt = BuildHeaderDictionary(lines.FirstOrDefault(),irr);
                      if (!headerBuilt)
                      {
                          return irr;
                      }
                      foreach (var item in lines.Skip(1))
                      {
                          string[] a = item.Split("\t".ToCharArray());
                          ImportItem ii = new ImportItem();
                          ii.FamilyPath = a[_headerIndex["familypath"]].Trim();
                          ii.GeneIdentifiere = a[_headerIndex["id"]].Trim();
                          ii.GeneName = a[_headerIndex["name"]].Trim();
                          ii.Species = a[_headerIndex["species"]].Trim();
                     //     ii.Database = a[_headerIndex["database"]].Trim();
                          ii.Sequence = a[_headerIndex["sequence"]].Trim();
                          ii.Description = a[_headerIndex["description"]].Trim();
                          ret.Add(ii);
                      }
                      return irr;
                  });
        }


        private Dictionary<string, int> _headerIndex;
        private bool BuildHeaderDictionary(string p, ImportReadResult irr)
        {
            _headerIndex = new Dictionary<string, int>();
            if (string.IsNullOrEmpty(p))
            {

                return false;
            }
            else
            {
                var split = p.Split("\t".ToCharArray()).ToList();
                split = split.Select(x => x.ToLower().Trim()).ToList();

                foreach (var h in _requiredHeader)
                {
                    if (split.Any(x => x == h))
                    {
                        _headerIndex.Add(h, split.IndexOf(h));
                    }
                    else
                    {
                        irr.HasErrors = true;
                        irr.Errors.Add("Missing required header ' " + h + "'");
                        return false;
                      
                    }
                }
                return true;
            }
        }

        public event EventHandler<ImportErrorEventArgs> ImportError;
    }
}
